/** @noSelfInFile **/

import {ColorStr} from "../../../../type/Type";
import {CodeUtil} from "../../../codeAdapter/CodeUtil";
import Configer from "../../config/Configer";
import {UIConfigKey} from "../../../../constant/UIConfig";
import {ResourceKey} from "../../../../constant/ResourceConstant";
import {frameAdapter, FrameEvent, IFrameAdapter, TextAlign} from "../../adapter/adapterInterface/model1/IFrameAdapter";

export class Style {
    backgroundImage?: string = '';
    // model?: string = '';

    height?: number = 0;
    width?: number = 0;

    top?: number = 0;
    left?: number = 0;

    border?: string
    borderWeight?: number = 0;

    marginT?: number = 0;
    marginB?: number = 0;
    marginL?: number = 0;
    marginR?: number = 0;

    paddingT?: number = 0;
    paddingB?: number = 0;
    paddingL?: number = 0;
    paddingR?: number = 0;

    float?: boolean = false;

    text?: string;
    fontAlign?: TextAlign = TextAlign.before;
    fontSize?: number = Configer.getUIConfig(UIConfigKey.fontSize);
    fontStyle?: string = Configer.getResource(ResourceKey.defaultTTf);
    color?: ColorStr = ColorStr.white;

    alpha?: number = 1;

    mask?: string = '';
    // light?: boolean = false;

    hidden?: boolean = false;

    scale?: number = 0;
}

/**
 BACKDROP, TEXT, GLUETEXTBUTTON, BUTTON, TEXTBUTTON, TEXTAREA,
 FRAME, CHECKBOX, EDITBOX, GLUEBUTTON, SCROLLBAR, SLIDER,
 POPUPMENU, CONTROL, DIALOG, HIGHLIGHT, LISTBOX, MENU,
 CHATDISPLAY, GLUECHECKBOX, SIMPLEFRAME, SIMPLEBUTTON;
 */
export class FrameModel {

    id: string;
    frameRecord: any;
    frameAdapter: IFrameAdapter;

    private propsTemp: Style = {
        backgroundImage: undefined,
        border: undefined,
        borderWeight: 0,
        color: undefined,
        float: false,
        text: undefined,
        fontAlign: undefined,
        fontSize: Configer.getUIConfig(UIConfigKey.fontSize),
        fontStyle: Configer.getResource(ResourceKey.defaultTTf),
        height: 0,
        left: 0,
        alpha:1,
        marginL: 0,
        marginT: 0,
        paddingL: 0,
        paddingT: 0,
        top: 0,
        width: 0,
        hidden: true,
    };

    constructor(id: string) {
        this.id = id;

        this.frameAdapter = frameAdapter.adapter;

        const parent = this.frameAdapter.getRoot();
        this.frameRecord = this.frameAdapter.createFrame(id, parent);
        // const disableBlp = Configer.getResource(ResourceKey.disableBlp);
        this.setFontStyle()
        this.setHidden();
    }


    public get style(): Style {
        return this.propsTemp;
    }

    public set style(style: Style) {
        this.propsTemp = style;
    }

    setCenter() {
        if (!CodeUtil.isNullOrZero(this.style.text)) {
            this.frameAdapter.setCenter(this.frameRecord, this.style.fontAlign)
        }
    }

    public setBorder() {
        this.frameAdapter.setBorder(this.frameRecord, this.style.border)
    }

    public setSize() {
        const padding: { t: number, r: number, b: number, l: number } = { t: this.style.paddingT, r: this.style.paddingR, b: this.style.paddingB, l: this.style.paddingL };
        this.frameAdapter.setSize(this.frameRecord, this.style.height, this.style.width, this.style.borderWeight, padding)
    }

    public get height() {
        return this.style.height;
    }

    public setText() {
        this.frameAdapter.setText(this.frameRecord, this.style.text, this.style.color)
    }

    public get text() {
        return this.style.text;
    }

    public set value(value: number) {
        this.frameAdapter.setValue(this.frameRecord, value)
    }

    public setMask(flag: number = 0) {
        this.frameAdapter.setMask(this.frameRecord, this.style.mask, flag)
    }

    public get hidden() {
        return !this.style.hidden;
    }

    public get width() {
        return this.style.width;
    }

    public cageMouse(enable: boolean) {
        this.frameAdapter.cageMouse(this.frameRecord, enable);
    }

    public clearPoints() {
        this.frameAdapter.clearPoints(this.frameRecord);
    }

    public destroy() {
        this.frameAdapter.destroy(this.frameRecord);
    }

    public setAbsPoint() {
        const padding: { t: number, r: number, b: number, l: number } = { t: this.style.paddingT, r: this.style.paddingR, b: this.style.paddingB, l: this.style.paddingL };
        const margin: { t: number, r: number, b: number, l: number } = { t: this.style.marginT, r: this.style.marginR, b: this.style.marginB, l: this.style.marginL };
        this.frameAdapter.setAbsPoint(this.frameRecord, this.style.left, this.style.top, this.style.borderWeight, margin, padding);
    }

    public setAlpha() {
        this.frameAdapter.setAlpha(this.frameRecord, this.style.alpha)
    }

    public setEnabled(flag: boolean) {
        this.frameAdapter.setEnabled(this.frameRecord, flag);
        return this;
    }

    public setFocus(flag: boolean) {
        this.frameAdapter.setFocus(this.frameRecord, flag);
        return this;
    }

    public setFontStyle() {
        this.frameAdapter.setFontStyle(this.frameRecord, this.style.fontStyle, this.style.fontSize);
    }

    public setMinMaxValue(minValue: number, maxValue: number) {
        this.frameAdapter.setMinMaxValue(this.frameRecord, minValue, maxValue)
    }

    /**
     * 设置模型（支持Sprite、Model、StatusBar）
     */
    public setModel(modelFile: string, modelType: number, flag: number = 0) {
        this.frameAdapter.setModel(this.frameRecord, modelFile, modelType, flag)
    }

    public getScale(): number {
        return this.style.scale;
    }

    public setScale(scale: number) {
        this.frameAdapter.setScale(this.frameRecord, scale);
    }


    public setSpriteAnimate(animId: number, autocast: boolean) {
        this.frameAdapter.setSpriteAnimate(this.frameRecord, animId, autocast);
    }

    public setStepSize(stepSize: number) {
        this.frameAdapter.setStepSize(this.frameRecord, stepSize);
    }

    public setBackGround() {
        this.frameAdapter.setBackImage(this.frameRecord, this.style.backgroundImage)
    }

    // /**
    //  * 创建一个背景图 并返回这个背景Frame
    //  * @param texFile
    //  * @param flag
    //  */
    // public addBackgroundImage(texFile: string, flag: number = 0) {
    //     let backdropFrame = new Frame("BACKDROP", `${this.id}`, this.current);
    //     backdropFrame.setTexture(texFile, flag);
    //     backdropFrame.setAllPoints(this.current)
    //     this.back = backdropFrame;
    // }

    public setVertexColor(color: number) {
        this.frameAdapter.setVertexColor(this.frameRecord, color);
    }

    public setHidden() {
        this.frameAdapter.setHidden(this.frameRecord, this.style.hidden, this.style.mask, this.style.backgroundImage, this.style.border, this.style.text)
    }

    /**
     * 事件
     */
    /**
     * 设置回调：点击事件
     */
    public setOnMouseDown(callback: (pos: FrameEvent) => boolean, sync = false) {
        this.frameAdapter.setOnMouseDown(this.frameRecord, callback, sync)
    }

    public setOnMouseRightDown(callback: (pos: FrameEvent) => boolean, sync = false) {
        this.frameAdapter.setOnMouseRightDown(this.frameRecord, callback, sync)
    }

    public stopMouseClick(bool: boolean) {
        this.frameAdapter.stopMouseClick(bool);
    }

    public setOnMouseUp(callback: (pos: FrameEvent) => boolean, sync = false) {
        this.frameAdapter.setOnMouseUp(this.frameRecord, callback, sync)
    }


    /**
     * 设置回调：双击事件
     */
    public setOnDoubleClick(callback: (pos: FrameEvent) => boolean, sync = false) {
        this.frameAdapter.setOnDoubleClick(this.frameRecord, callback, sync)
    }

    /**
     * 设置回调：鼠标进入
     */
    public setOnMouseEnter(callback: (pos: FrameEvent) => boolean, sync = false) {
        this.frameAdapter.setOnMouseEnter(this.frameRecord, callback, sync)
    }

    /**
     * 设置回调：鼠标离开
     */
    public setOnMouseLeave(callback: (pos: FrameEvent) => boolean, sync = false) {
        this.frameAdapter.setOnMouseLeave(this.frameRecord, callback, sync)
    }

    /**
     * 设置回调：鼠标滚轮
     */
    public setOnMouseWheel(callback: (pos: FrameEvent) => boolean, sync = false) {
        this.frameAdapter.setOnMouseWheel(this.frameRecord, callback, sync)
    }
}
